﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Alsing.SourceCode;
using UmlControlLibrary;

namespace UmlControlLibrary
{
    /// <summary>
    /// Interaction logic for UmlPropertyProperties.xaml
    /// </summary>
    public partial class UmlPropertyProperties : Window
    {
        private static ResourceDictionary theme = null;
        private const string ThemeUri = @"PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component\themes/aero.normalcolor.xaml";
        // private const string ThemeUri = @"PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component\themes/luna.normalcolor.xaml";
        // private const string ThemeUri = @"PresentationFramework.Royale;V3.0.0.0;31bf3856ad364e35;component\themes/royale.normalcolor.xaml";

        
        private string defaultGetter;
        private string defaultSetter;

        private UmlProperty _property;
        public UmlProperty Property
        {
            get
            {
                return _property;
            }
            set
            {
                _property = value;
                DataContext = _property;
                string code = _property.CustomGetter;
                if (code.Trim().Length == 0)
                {
                    defaultGetter = GetGetterCode(_property.ElementName);
                    code = defaultGetter;
                }
                CustomCode.SetText("getter", code);
                CustomCode.OnTextChanged += new TextChangeDelegate(CustomCode_OnTextChanged);

                code = _property.CustomSetter;
                if (code.Trim().Length == 0)
                {
                    defaultSetter = GetSetterCode(_property.ElementName);
                    code = defaultSetter;
                }
                CustomCode.SetText("setter", code);

                Property.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Property_PropertyChanged);
            }
        }

        bool IsDefaultGetter()
        {
            string s = CustomCode.GetText("getter").Trim();
            return ((s.Length == 0) || (s == defaultGetter));
        }

        bool IsDefaultSetter()
        {
            string s = CustomCode.GetText("setter").Trim();
            return ((s.Length == 0) || (s == defaultSetter));
        }

        void CustomCode_OnTextChanged(SyntaxDocument sender, string DocumentName)
        {
/*            if (DocumentName == "getter")
                isDefaultGetter = sender.Text == GetGetterCode(Property.ElementName);
            if (DocumentName == "setter")
                isDefaultSetter = sender.Text == GetSetterCode(Property.ElementName);
            */
            CustomCode.SetWarningFlag(DocumentName, sender.Text.Trim().Length == 0);
        }

        void Property_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "PropertyName")
            {
                if (IsDefaultGetter())
                {
                    defaultGetter = GetGetterCode(Property.ElementName);
                    CustomCode.SetText("getter", defaultGetter);
                }
                if (IsDefaultSetter())
                {
                    defaultSetter = GetSetterCode(Property.ElementName);
                    CustomCode.SetText("setter", defaultSetter);
                }
            }
        }

        private string GetGetterCode(string propertyName)
        {
            return "return m_" + propertyName + ";";
        }

        private string GetSetterCode(string propertyName)
        {
            return "m_" + propertyName + " = value;";
        }

        public UmlPropertyProperties()
        {
            InitializeComponent();
            if (theme == null)
            {
                Uri uri = new Uri(ThemeUri, UriKind.Relative);
                theme = Application.LoadComponent(uri) as ResourceDictionary;
            }

            this.Resources.MergedDictionaries.Add(theme);
            CustomCode.SetSyntaxFile("CSharp");
            CustomCode.AddItem("getter", "Custom Getter", "");
            CustomCode.AddItem("setter", "Custom Setter", "");
            TypeTreeView.DataContext = UmlModel.GetInstance();
        }

        private void PropertyContextMenu_TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (sender is TextBox)
                UpdateRoundTrip(sender as TextBox, TextBox.TextProperty);
            else if (sender is ComboBox)
                UpdateRoundTrip(sender as ComboBox, ComboBox.TextProperty);
        }

        public static void UpdateRoundTrip(Control control, DependencyProperty prop)
        {
            BindingExpression expr = control.GetBindingExpression(prop);
            expr.UpdateSource();
            expr.UpdateTarget();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            if (IsDefaultGetter())
                _property.CustomGetter = defaultGetter;
            else
                _property.CustomGetter = CustomCode.GetText("getter");

            if (IsDefaultSetter())
                _property.CustomSetter = defaultSetter;
            else
                _property.CustomSetter = CustomCode.GetText("setter");

            _property.TypeName = TypeCombo.Text;

            this.Close();
        }

        private void UmlTreeView_Selected(object sender, RoutedEventArgs e)
        {
            TypeCombo.Text = TypeTreeView.ToString();// ((e.OriginalSource as TreeViewItem).Header as UmlElement).Name;
        }

        private void TypeCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
//            UmlElement element = (UmlElement)(TypeTreeView.RootTreeView.Tree.SelectedItem as TreeViewItem).Header;
//            TypeCombo.Text = element.Name;
     //       TypeCombo.SelectedValue = TypeTreeView;
        }
    }
}
